import java.awt.Color;

import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;
import uchicago.src.sim.space.Object2DGrid;


/**
 * Class that implements the simulation agent for the rabbits grass simulation.
 * @author Liansheng HUA & Yujie GUO
 */

public class RabbitsGrassSimulationAgent implements Drawable {

	private int energyLevel;
	private int xPosition;
	private int yPosition;
	private RabbitsGrassSimulationSpace space;
	private int vX;
	private int vY;

	public RabbitsGrassSimulationAgent(int initialEnergy){
		xPosition = -1;
		yPosition = -1;
		energyLevel = 0;
		this.energyLevel = initialEnergy;
	}

	public void setEnergyLevel(int energyLevel) {
		this.energyLevel = energyLevel;
	}

	public int getEnergyLevel() {
		return this.energyLevel;
	}

	public void draw(SimGraphics G) {
		G.drawFastRoundRect(Color.white);
	}

	public void setXY(int newX, int newY){
		xPosition = newX;
		yPosition = newY;
	}

	public void setSpace(RabbitsGrassSimulationSpace s){
		space = s;
	}

	public int getX() {
		return xPosition;
	}

	public int getY() {
		return yPosition;
	}


	public void setVxVy() {
		vX = 0;
		vY = 0;
		while((vX == 0) && ( vY == 0)){
			vX = (int)Math.floor(Math.random() * 3) - 1;
			vY = (int)Math.floor(Math.random() * 3) - 1;
		}
	}

	public void move(int rabbitMoveEnergy) {
		int kmax = 100;
		int newX, newY;
		Object2DGrid grid = space.getCurrentRabbitSpace();
	
		for (int k = 0; k < kmax; k++) {
			setVxVy();
			newX = (xPosition + vX + grid.getSizeX()) % grid.getSizeX();
			newY = (yPosition + vY + grid.getSizeY()) % grid.getSizeY();
			if (space.moveAgentAt(xPosition, yPosition, newX, newY)) {
				energyLevel -= rabbitMoveEnergy;
				break;
			}
		}
	}

	public void eatGrass(int grassEnergyLevel) {
		energyLevel += space.eatGrassAt(xPosition, yPosition) * grassEnergyLevel;
	}

}
